<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>设备编辑</title>
    <link rel="stylesheet" href="../../assets/css/bootstrap.min.css">
    <link rel="stylesheet" href="../../../css/style.css">
    <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
    <style>
        body {
            font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
            background-color: #f7f7f7;
            color: #333;
        }

        .admin-layout {
            background-color: #f7f7f7;
        }

        .content-wrapper {
            background-color: #fff;
            border-radius: 12px;
            box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
            padding: 24px;
            margin: 20px;
        }

        .content-header {
            margin-bottom: 24px;
            border-bottom: 1px solid #f0f0f0;
            padding-bottom: 16px;
        }

        #pageTitle {
            font-size: 24px;
            font-weight: 600;
            color: #333;
            margin: 0;
        }

        .form-container {
            max-width: 600px;
            margin: 0 auto;
        }

        .form-group {
            margin-bottom: 20px;
            position: relative;
        }

        .form-label {
            font-size: 14px;
            font-weight: 500;
            color: #666;
            margin-bottom: 8px;
            display: block;
        }

        .required-field::after {
            content: "*";
            color: #dc3545;
            margin-left: 4px;
        }

        .form-control, .form-select {
            height: 44px;
            border-radius: 10px;
            border: 1px solid #e0e0e0;
            padding: 0 16px;
            font-size: 16px;
            transition: all 0.2s ease;
            width: 100%;
            background-color: #f9f9f9;
        }

        .form-control:focus, .form-select:focus {
            border-color: #28a745;
            box-shadow: 0 0 0 3px rgba(40, 167, 69, 0.1);
            background-color: #fff;
            outline: none;
        }

        .btn {
            height: 44px;
            border-radius: 10px;
            font-size: 16px;
            font-weight: 500;
            padding: 0 20px;
            transition: all 0.2s ease;
        }

        .btn-primary {
            background-color: #28a745;
            border-color: #28a745;
        }

        .btn-primary:hover {
            background-color: #218838;
            border-color: #1e7e34;
        }

        .btn-secondary {
            background-color: #f2f2f7;
            border-color: #f2f2f7;
            color: #28a745;
        }

        .btn-secondary:hover {
            background-color: #e5e5ea;
            border-color: #e5e5ea;
        }

        .btn-action {
            min-width: 120px;
        }

        .submenu {
            display: none;
        }

        .menu-item.active > .submenu {
            display: block;
        }

        .menu-arrow {
            transition: transform 0.2s;
        }

        .menu-item.active > a > .menu-arrow {
            transform: rotate(90deg);
        }

        .submenu li a.active {
            color: #28a745;
            font-weight: bold;
            transform: translateY(2px);
            display: inline-block;
        }

        @media (max-width: 768px) {
            .content-wrapper {
                padding: 16px;
                margin: 10px;
            }

            .form-container {
                padding: 0;
            }
        }
        /* iOS风格模态框样式 */
        .ios-modal {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: rgba(0, 0, 0, 0.4);
            backdrop-filter: blur(10px);
            -webkit-backdrop-filter: blur(10px);
            display: none;
            justify-content: center;
            align-items: center;
            z-index: 1050;
            opacity: 0;
            transition: opacity 0.3s ease;
        }

        .ios-modal.show {
            display: flex;
            opacity: 1;
        }

        .ios-modal-content {
            background: rgba(255, 255, 255, 0.95);
            backdrop-filter: blur(20px);
            -webkit-backdrop-filter: blur(20px);
            border-radius: 20px;
            padding: 0;
            max-width: 400px;
            width: 90%;
            max-height: 80vh;
            overflow: hidden;
            box-shadow: 0 20px 40px rgba(0, 0, 0, 0.15);
            transform: scale(0.8);
            transition: transform 0.3s ease;
        }

        .ios-modal.show .ios-modal-content {
            transform: scale(1);
        }

        .ios-modal-header {
            padding: 20px 20px 10px;
            text-align: center;
            border-bottom: 1px solid rgba(0, 0, 0, 0.1);
        }

        .ios-modal-title {
            font-size: 18px;
            font-weight: 600;
            color: #333;
            margin: 0;
        }

        .ios-modal-body {
            padding: 20px;
            max-height: 60vh;
            overflow-y: auto;
        }

        .ios-detail-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 12px 0;
            border-bottom: 1px solid rgba(0, 0, 0, 0.05);
        }

        .ios-detail-item:last-child {
            border-bottom: none;
        }

        .ios-detail-label {
            font-weight: 500;
            color: #666;
            font-size: 14px;
        }

        .ios-detail-value {
            font-weight: 400;
            color: #333;
            font-size: 14px;
            text-align: right;
            max-width: 60%;
            word-wrap: break-word;
        }

        .ios-modal-footer {
            padding: 15px 20px 20px;
            text-align: center;
        }

        .ios-close-btn {
            background: #007AFF;
            color: white;
            border: none;
            border-radius: 12px;
            padding: 12px 30px;
            font-size: 16px;
            font-weight: 500;
            cursor: pointer;
            transition: background 0.2s ease;
        }

        .ios-close-btn:hover {
            background: #0056CC;
        }

        /* 滚动条样式 */
        .ios-modal-body::-webkit-scrollbar {
            width: 4px;
        }

        .ios-modal-body::-webkit-scrollbar-track {
            background: transparent;
        }

        .ios-modal-body::-webkit-scrollbar-thumb {
            background: rgba(0, 0, 0, 0.2);
            border-radius: 2px;
        }
    </style>
</head>
<body>
<div class="admin-layout">
    <!-- 顶部导航栏 -->
    <header class="admin-header">
        <div class="header-left">
            <div class="logo">生产过程管理系统</div>
        </div>
        <div class="header-right">
            <span class="user-info">管理员</span>
            <span class="user-name">
                    <script>
                        try {
                            const userInfo = localStorage.getItem('userInfo');
                            const user = JSON.parse(userInfo);
                            document.write(user.name || '未登录');
                        } catch (e) {
                            document.write('未登录');
                        }
                    </script>
                </span>
            <button class="logout-btn" onclick="logout()">退出</button>
        </div>
    </header>
    <!-- 左侧菜单 -->
    <aside class="admin-sidebar">
        <ul class="menu-list">
            <li class="menu-item">
                <a href="../homepage/administrator_home.html">
                    <span class="menu-icon">🏠</span>
                    <span class="menu-title">首页</span>
                </a>
            </li>
            <li class="menu-item">
                <a href="javascript:void(0)">
                    <span class="menu-icon">👥</span>
                    <span class="menu-title">人员管理</span>
                    <span class="menu-arrow"></span>
                </a>
                <ul class="submenu">
                    <li><a href="../person/person_list.html">人员列表</a></li>
                    <li><a href="../person/person_edit.html">添加人员</a></li>
                </ul>
            </li>
            <li class="menu-item">
                <a href="javascript:void(0)">
                    <span class="menu-icon">📦</span>
                    <span class="menu-title">物料管理</span>
                    <span class="menu-arrow"></span>
                </a>
                <ul class="submenu">
                    <li><a href="../material/material_list.html">物料列表</a></li>
                    <li><a href="../material/material_edit.html">添加物料</a></li>
                </ul>
            </li>
            <li class="menu-item active">
                <a href="javascript:void(0)">
                    <span class="menu-icon">⚙️</span>
                    <span class="menu-title">设备管理</span>
                    <span class="menu-arrow"></span>
                </a>
                <ul class="submenu">
                    <li><a href="device_list.html">设备列表</a></li>
                    <li><a href="../device/device_edit.html" class="active">添加设备</a></li>
                </ul>
            </li>
            <li class="menu-item">
                <a href="javascript:void(0)">
                    <span class="menu-icon">📝</span>
                    <span class="menu-title">产品管理</span>
                    <span class="menu-arrow"></span>
                </a>
                <ul class="submenu">
                    <li><a href="../product/product_list.html">产品列表</a></li>
                    <li><a href="../product/product_edit.html">添加产品</a></li>
                </ul>
            </li>
            <li class="menu-item">
                <a href="javascript:void(0)">
                    <span class="menu-icon">🏢</span>
                    <span class="menu-title">供应商管理</span>
                    <span class="menu-arrow"></span>
                </a>
                <ul class="submenu">
                    <li><a href="../provider/provider_list.html">供应商列表</a></li>
                    <li><a href="../provider/provider_edit.html">添加供应商</a></li>
                </ul>
            </li>
        </ul>
    </aside>
    <!-- 主内容区域 -->
    <main class="admin-main">
        <div class="content-wrapper">
            <div class="content-header">
                <h2 id="pageTitle">添加设备</h2>
            </div>
            <div class="content-body">
                <div class="form-container">
                    <form id="deviceForm">
                        <input type="hidden" id="deviceId">
                        <div class="form-group">
                            <label for="deviceNumber" class="form-label">设备编号</label>
                            <input type="text" class="form-control" id="deviceNumber" readonly disabled
                                   placeholder="设备编号将自动生成">
                            <small class="text-muted">设备编号将在保存后自动生成</small>
                        </div>
                        <div class="form-group">
                            <label for="deviceName" class="form-label required-field">设备名称</label>
                            <input type="text" class="form-control" id="deviceName" required>
                        </div>
                        <div class="form-group">
                            <label for="deviceType" class="form-label required-field">设备类型</label>
                            <select class="form-select" id="deviceType" required>
                                <option value="">-- 请选择类型 --</option>
                                <option value="重型">重型</option>
                                <option value="中型">中型</option>
                                <option value="轻型">轻型</option>
                                <option value="便携式">便携式</option>
                                <option value="其它">其它</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="manufacturer" class="form-label required-field">制造商</label>
                            <input type="text" class="form-control" id="manufacturer" required>
                        </div>
                        <div class="form-group">
                            <label for="manufactureDate" class="form-label required-field">生产日期</label>
                            <input type="date" class="form-control" id="manufactureDate" required>
                        </div>
                        <div class="form-group">
                            <label for="warrantyPeriod" class="form-label">保修期限</label>
                            <input type="text" class="form-control" id="warrantyPeriod">
                        </div>
                        <div class="form-group">
                            <label for="afterSalesContact" class="form-label">售后联系人</label>
                            <input type="text" class="form-control" id="afterSalesContact">
                        </div>
                        <div class="form-group">
                            <label for="afterSalesPhone" class="form-label">售后电话</label>
                            <input type="text" class="form-control" id="afterSalesPhone">
                        </div>
                        <div class="form-group">
                            <label for="warrantyDesc" class="form-label">保修说明</label>
                            <textarea class="form-control" id="warrantyDesc" rows="3"></textarea>
                        </div>
                        <div class="d-flex justify-content-between mt-4">
                            <button type="button" class="btn btn-secondary btn-action" onclick="goBack()">返回</button>
                            <button type="submit" class="btn btn-primary btn-action">保存</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </main>
</div>
<script src="../../assets/js/bootstrap.bundle.min.js"></script>
<script>
    let isEditMode = false;
    let deviceId = null;
    document.addEventListener('DOMContentLoaded', function () {
        var menuItems = document.querySelectorAll('.menu-item > a');
        menuItems.forEach(function (item) {
            item.addEventListener('click', function (e) {
                var parent = item.parentElement;
                document.querySelectorAll('.menu-item').forEach(function (el) {
                    if (el !== parent) el.classList.remove('active');
                });
                parent.classList.toggle('active');
                e.preventDefault();
            });
        });
        // 添加首页链接的点击事件
        document.querySelector('.menu-item:first-child > a').addEventListener('click', function (e) {
            e.preventDefault(); // 阻止默认行为
            window.location.href = '../homepage/administrator_home.html'; // 跳转到管理员首页
        });
        // 默认展开设备管理菜单
        const deviceMenuItem = document.querySelector('.menu-item:nth-child(4)');
        if (deviceMenuItem) {
            deviceMenuItem.classList.add('active');
        }
        // 设置当前页面对应的菜单项为选中状态
        const currentPath = window.location.pathname;
        const filename = currentPath.substring(currentPath.lastIndexOf('/') + 1);
        document.querySelectorAll('.submenu li a').forEach(link => {
            if (link.getAttribute('href') === filename) {
                link.classList.add('active');
            }
        });
        // 检查URL参数，判断是编辑还是新增
        const urlParams = new URLSearchParams(window.location.search);
        deviceId = urlParams.get('id');
        if (deviceId) {
            isEditMode = true;
            document.getElementById('pageTitle').textContent = '编辑设备';
            document.getElementById('deviceId').value = deviceId;
            loadDeviceData(deviceId);
        } else {
            document.getElementById('pageTitle').textContent = '添加设备';
        }
        document.getElementById('deviceForm').addEventListener('submit', function (e) {
            e.preventDefault();
            saveDevice();
        });
    });

    async function loadDeviceData(id) {
        try {
            const response = await fetch(`http://localhost:8080/api/devices/${id}`);
            if (!response.ok) throw new Error('获取设备数据失败');
            const device = await response.json();
            document.getElementById('deviceNumber').value = device.deviceNumber || '';
            document.getElementById('deviceName').value = device.deviceName || '';
            document.getElementById('deviceType').value = device.deviceType || '';
            document.getElementById('manufacturer').value = device.manufacturer || '';
            document.getElementById('manufactureDate').value = device.manufactureDate || '';
            document.getElementById('warrantyPeriod').value = device.warrantyPeriod || '';
            document.getElementById('afterSalesContact').value = device.afterSalesContact || '';
            document.getElementById('afterSalesPhone').value = device.afterSalesPhone || '';
            document.getElementById('warrantyDesc').value = device.warrantyDesc || '';
        } catch (error) {
            console.error('加载设备数据失败:', error);
            alert('加载设备数据失败，请稍后重试');
        }
    }

    async function saveDevice() {
        const form = document.getElementById('deviceForm');
        if (!form.checkValidity()) {
            form.reportValidity();
            return;
        }
        const deviceData = {
            deviceName: document.getElementById('deviceName').value,
            deviceType: document.getElementById('deviceType').value,
            manufacturer: document.getElementById('manufacturer').value,
            manufactureDate: document.getElementById('manufactureDate').value,
            warrantyPeriod: document.getElementById('warrantyPeriod').value,
            afterSalesContact: document.getElementById('afterSalesContact').value,
            afterSalesPhone: document.getElementById('afterSalesPhone').value,
            warrantyDesc: document.getElementById('warrantyDesc').value
        };

        // 如果是编辑模式，添加设备编号
        if (isEditMode) {
            deviceData.deviceNumber = document.getElementById('deviceNumber').value;
            deviceData.id = parseInt(deviceId);
        }

        try {
            let url = 'http://localhost:8080/api/devices';
            let method = 'POST';
            if (isEditMode && deviceId) {
                url = `${url}/${deviceId}`;
                method = 'PUT';
            }
            const response = await fetch(url, {
                method: method,
                headers: {'Content-Type': 'application/json'},
                body: JSON.stringify(deviceData)
            });
            if (!response.ok) throw new Error('保存设备数据失败');
            const result = await response.json();
            alert(isEditMode ? '设备信息更新成功' : '设备添加成功');
            window.location.href = 'device_list.html';
        } catch (error) {
            console.error('保存设备数据失败:', error);
            alert('保存设备数据失败，请稍后重试');
        }
    }

    function goBack() {
        window.location.href = 'device_list.html';
    }

    function logout() {
        // 添加确认提示
        if (confirm('确定要退出登录吗？')) {
            // 清除本地存储的登录信息
            localStorage.removeItem('userInfo');
            localStorage.removeItem('token');

            // 跳转到登录页面
            window.location.href = 'login.html';
        }
    }
</script>
</body>
</html>